
import {
  setToken,
  setUserId,
  setUsername,
  setRoleIds,
  getToken,
  getUsername,
  getUserId,
  getRoleIds,
  removeUsername,
  removeToken,
  removeRoleIds,
  removeUserId
} from '../utils/localStorage'

export default {
  state: {

    token: '',
    username: '',
    userId: -1,
    roleIds:[],

  },

  getters:{
    token:function (state) {
      if(!state.token){
        state.token = getToken();
      }
      return state.token;
    },

    username:function (state) {
      if(!state.username){
        state.username = getUsername();
      }
      return state.username;
    },

    userId:function (state) {
      if(!state.userId){
        state.userId = getUserId();
      }
      return state.userId;
    },

    roleIds:function (state) {
      if(!state.roleIds){
        state.roleIds = getRoleIds();
      }
      return state.roleIds;
    }
  },

  mutations: {
    setUsername:function (state,username) {
      state.username = username;
      setUsername(username);
    },

    setUserId:function (state,userId) {
      state.userId = userId;
      setUserId(userId);
    },

    setToken:function (state,token) {
      state.token = token;
      setToken(token);
    },
    setRoleIds:function (state,roleIds) {
      state.roleIds = roleIds;
      setRoleIds(roleIds)
    },

    logout:function (state) {
      state.username='';
      state.userId='';
      state.roleIds=[];
      state.token='';
      removeToken();
      removeUserId();
      removeRoleIds();
      removeUsername();

    }
  },

  actions:{
    setUsername:function (context,username) {
      context.commit('setUsername',username);
    },

    setUserId: function (context,userId) {
      context.commit('setUserId',userId);
    },

    setToken: function (context,token) {
      context.commit('setToken',token);
    },

    setRoleIds:function (context,roleIds) {
      context.commit('setRoleIds',roleIds)
    },

    logout:function (context) {

      context.commit('logout');
    }
  }
}
